package com.polaris.live.adapter.recharge

import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.PayConst
import com.polaris.live.databinding.AdapterExPopupRechargeBinding
import com.polaris.live.databinding.AdapterPopupRechargeBinding
import com.polaris.live.resp.back_resp.PayerRechargePriceListVo
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * QuickRechargeAdapter
 *
 * @author Created by 半仙 on 2023/10/17/017 14:12
 *
 */

class QuickRechargeAdapter : BaseAdapter<PayerRechargePriceListVo, BaseViewHolder>(0) {
    override fun getDefItemViewType(position: Int): Int {
        return if (data[position].extraReward != null) {
            HAVE_EXTRAREWARD
        } else {
            NO_EXTRAREWARD
        }
    }


    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            HAVE_EXTRAREWARD -> HaveViewHolder(
                AdapterExPopupRechargeBinding.inflate(layoutInflater, parent, false)
            )

            NO_EXTRAREWARD -> NoViewHolder(
                AdapterPopupRechargeBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }


    override fun convert(holder: BaseViewHolder, item: PayerRechargePriceListVo) {
        when (holder) {
            is HaveViewHolder -> holder.bind(item)
            is NoViewHolder -> holder.bind(item)
        }
    }


    inner class HaveViewHolder(val binding: AdapterExPopupRechargeBinding) :
        BaseBindingViewHolder<AdapterExPopupRechargeBinding, PayerRechargePriceListVo>(binding) {
        override fun bind(item: PayerRechargePriceListVo) {
            binding.apply {
                notPriceText.paintFlags = notPriceText.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                rechargeItemNeedMoney.text = "\$${item.price}"

                if (item.coin == 0L) {
                    notPriceText.visibility = View.GONE
                } else {
                    notPriceText.visibility = View.VISIBLE
                    notPriceText.text = "${item.coin}"
                }

                rechargeItemExGratia.visibility = View.VISIBLE
                rechargeItemExGratiaImg.visibility = View.VISIBLE
                rechargeItemExGratiaText.visibility = View.VISIBLE
                rechargeBack.setBackgroundResource(R.drawable.r8_back_ffffff)

                if (item.giveCoin == 0L) {
                    okPriceText.visibility = View.GONE
                    notPriceText.visibility = View.GONE
                    rechargeItemExGratiaText.visibility = View.GONE
                    rechargeItemExGratiaImg.visibility = View.GONE
                    rechargeItemExGratia.visibility = View.GONE
                } else {
                    okPriceText.visibility = View.VISIBLE
                    okPriceText.text = "+${item.giveCoin}"
                }

                rechargeItemText.text = "${(item.coin + item.giveCoin)}"

                Glide.with(context).load(ImageUtils.imgPath(item.icon)).into(ivCoin)

                item.extraReward?.let {
                    Glide.with(context).load(ImageUtils.imgPath(it.icon)).into(ivCoinEx)
                    rechargeItemTextEx.text = "${it.coin}"
                    rechargeExCount.text = "X${it.number}"
                }
                when (item.payerRechargeRewardsType) {
                    PayConst.RechargeListType.FIRST_CHARGE -> {
                        if (layoutPosition == 0) {
                            rechargeBack.setBackgroundResource(R.drawable.r8_gradient_fffed8_fff6bf)
                        }
                    }
                }
            }
        }

    }


    inner class NoViewHolder(val binding: AdapterPopupRechargeBinding) :
        BaseBindingViewHolder<AdapterPopupRechargeBinding, PayerRechargePriceListVo>(binding) {
        override fun bind(item: PayerRechargePriceListVo) {
            binding.apply {
                notPriceText.paintFlags = notPriceText.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                rechargeItemNeedMoney.text = "\$${item.price}"

                if (item.coin == 0L) {
                    notPriceText.visibility = View.GONE
                } else {
                    notPriceText.visibility = View.VISIBLE
                    notPriceText.text = "${item.coin}"
                }

                rechargeItemExGratia.visibility = View.VISIBLE
                rechargeItemExGratiaImg.visibility = View.VISIBLE
                rechargeItemExGratiaText.visibility = View.VISIBLE
                rechargeBack.setBackgroundResource(R.drawable.r8_back_ffffff)

                if (item.giveCoin == 0L) {
                    okPriceText.visibility = View.GONE
                    notPriceText.visibility = View.GONE
                    rechargeItemExGratiaText.visibility = View.GONE
                    rechargeItemExGratiaImg.visibility = View.GONE
                    rechargeItemExGratia.visibility = View.GONE
                } else {
                    okPriceText.visibility = View.VISIBLE
                    okPriceText.text = "+${item.giveCoin}"
                }

                rechargeItemText.text = "${(item.coin + item.giveCoin)}"

                Glide.with(context).load(ImageUtils.imgPath(item.icon)).into(ivCoin)

                when (item.payerRechargeRewardsType) {
                    PayConst.RechargeListType.FIRST_CHARGE -> {
                        if (layoutPosition == 0) {
                            rechargeBack.setBackgroundResource(R.drawable.r8_gradient_fffed8_fff6bf)
                        }
                    }
                }
            }

        }

    }


    companion object {
        const val HAVE_EXTRAREWARD = 1 //有附加赠送
        const val NO_EXTRAREWARD = 2   //没有附加赠送

    }


}